package com.study.stream;

import java.io.IOException;
import java.util.*;

/**
 * @author study
 * @version 1.0
 * @date 2020/6/16 15:49
 */
public class Cal {
    /**
     * java 8的差集
     *https://blog.csdn.net/weixin_38256991/article/details/81672235
     */
    /**
     * 高效求两个list的差集
     * https://blog.csdn.net/Garlic246/article/details/85165053
     *
     * @param args
     * @throws IOException
     */
    public static void main1(String[] args) throws IOException {
        List<String> listA = new ArrayList();
        List<String> listB = new ArrayList();
        for (int i = 0; i < 1000000; i++) {
            listA.add(UUID.randomUUID().toString().hashCode() + i + "");
        }
        for (int i = 0; i < 1000000; i++) {
            listB.add(i + UUID.randomUUID().toString().hashCode() + "");
        }
        long startTime = System.currentTimeMillis();
        listA.removeAll(listB);
        long endTime = System.currentTimeMillis();

        System.out.println("程序运行时间： " + (endTime - startTime) + "ms");
    }

    public static void main(String[] args) {
        List<String> aList = new ArrayList<>();
        aList.addAll(Arrays.asList("a", "b"));

        List<String> bList = new ArrayList<>();
        bList.addAll(Arrays.asList("c", "b", "d"));

        System.out.println(difference(aList,bList));
        /*aList.removeAll(bList);
        System.out.println(aList);//[a]*/

        //TODO b集合 中有 A 集合 没有
      /*  bList.removeAll(aList);
        System.out.println(bList);*/

    }

    static List<String> getList(String name) {
        List<String> aList = new ArrayList<>();
        if ("a".equals(name)) {
            aList.addAll(Arrays.asList("a", "b"));
            return aList;
        }
        aList.addAll(Arrays.asList("c", "b"));
        return aList;
    }

    /**
     * 求差集
     *
     * @param aList a集合
     * @param bList b
     * @return
     */
    public static List<String> difference(List<String> aList, List<String> bList) {
        HashMap<String, Boolean> map = new HashMap();
        for (String tmp : aList) {
            map.put(tmp, true);
        }
        aList.clear();

        for (String bStr : bList) {
            if (!map.containsKey(bStr)) {
                aList.add(bStr);
            }
        }
        return aList;
    }

    public static void main5(String[] args) throws IOException {
        List<String> listA = new ArrayList();
        List<String> listB = new ArrayList();
        for (int i = 0; i < 1000000; i++) {
            listA.add(UUID.randomUUID().toString().hashCode() + i + "");
        }
        for (int i = 0; i < 1000000; i++) {
            listB.add(i + UUID.randomUUID().toString().hashCode() + "");
        }
        long startTime = System.currentTimeMillis();
        HashMap<String, Boolean> map = new HashMap();
        for (int i = 0; i < 1000000; i++) {
            map.put(listA.get(i), true);
        }
        listA.clear();
        for (int i = 0; i < 1000000; i++) {
            String x = listB.get(i);
            if (!map.containsKey(x)) {
                listA.add(x);
            }
        }
        long endTime = System.currentTimeMillis();

        System.out.println("程序运行时间： " + (endTime - startTime) + "ms");
    }
}
